//
//  NewsTableHeaderView.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/9/30.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "NewsTableHeaderView.h"
#import "NetEaseImgView.h"
#import "UIImageView+WebCache.h"

#import "NewsViewController.h"
#import "PhotoShowViewController.h"
#import "NewsDetailViewController.h"

#define scrollViewHeight self.bounds.size.height-30
#define contentLabelHeight 30

@implementation NewsTableHeaderView {
    UIScrollView* _scrollView;           //滑动视图
    UIImageView* _smallImgView;          //小图标
    UILabel* _newsContentLabel;          //新闻内容简介
    
    UIPageControl* _pageControl;         //配合滑动视图显示的页面点
    
    NSArray* _headerNewsArray;           //头条图片的新闻数据数组
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, scrollViewHeight)];
        _scrollView.pagingEnabled = YES;
        _scrollView.backgroundColor = [UIColor clearColor];
        _scrollView.delegate = self;         //滑动的时候要切换 新闻简介label 的显示，以及pagecontrol的显示
        
        _smallImgView = [[UIImageView alloc] initWithFrame:CGRectMake(15, scrollViewHeight+7.5, contentLabelHeight-15, contentLabelHeight-15)];
        _smallImgView.image = [UIImage imageNamed:@"usercenter_hd_album"];
        _smallImgView.contentMode = UIViewContentModeScaleAspectFit;
        
        _newsContentLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, scrollViewHeight, kWidth-100, contentLabelHeight)];
        _newsContentLabel.font = [UIFont systemFontOfSize:14];
        _newsContentLabel.textColor = [UIColor blackColor];
        
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(kWidth-60, _scrollView.height, 40, contentLabelHeight)
                       ];
        _pageControl.currentPageIndicatorTintColor = kNetEaseRedColor;
        _pageControl.pageIndicatorTintColor = [UIColor grayColor];
        
        
        [self addSubview:_scrollView];
        [self addSubview:_smallImgView];
        [self addSubview:_newsContentLabel];
        [self addSubview:_pageControl];
        
        
        //添加手势，单击进入图集或新闻详情
        UITapGestureRecognizer* tapRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToPhotoSetVCAction)];
        [self addGestureRecognizer:tapRec];
    }
    
    return self;
}

- (void)setNewsModel:(NewsModel *)newsModel {
    _newsModel = newsModel;
    
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    _headerNewsArray = _newsModel.headerNewsArray;
    
    if ([_newsModel.newsType isEqualToString:@"时尚"]) {
        //如果是时尚页，第一条新闻跟头条一样，不知道咋回事，就对不住了
        _headerNewsArray = nil;
    } else if ([_newsModel.newsType isEqualToString:@"娱乐"]) {
        _headerNewsArray = nil;
        _headerNewsArray = [NSArray arrayWithArray:_newsModel.headNewsImgArray];
    }
    
    //先删除所有子视图,因为复用问题
    for (UIView* view in _scrollView.subviews) {
        [view removeFromSuperview];
    }
    if (_headerNewsArray.count) {
        //滑动视图
        _scrollView.contentSize = CGSizeMake(kWidth*_headerNewsArray.count, scrollViewHeight);
        
        for (int i = 0; i < _headerNewsArray.count; i++) {
            NSDictionary* dic = _headerNewsArray[i];
            NSString* imgUrl = dic[@"imgsrc"];
            NetEaseImgView* imgView = [[NetEaseImgView alloc] initWithFrame:CGRectMake(kWidth*i, 0, kWidth, scrollViewHeight)];
//            imgView.contentMode = UIViewContentModeScaleAspectFit;
            imgView.userInteractionEnabled = YES;
            if (imgUrl) {
                [imgView sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
            } else {            //如果图片不存在，就用默认的 有 “网易” 字样的图片
                [imgView setBgImg];
            }
            
            [_scrollView addSubview:imgView];
        }
        
        //新闻简介.首先先显示第一个新闻数据的内容
        NSString* contentStr = [_headerNewsArray[0] objectForKey:@"title"];
        _newsContentLabel.text = contentStr;
        //剩余的等scrollView滑动的时候再切换显示
        
        //pageControl
        _pageControl.numberOfPages = _headerNewsArray.count;
        _pageControl.hidden = _pageControl.numberOfPages>1?NO:YES;      //只有一张图就隐藏pagecontrol
        _pageControl.currentPage = 0;
        
    } else {                        //如果这个数组为空
        _scrollView.contentSize = CGSizeMake(kWidth, scrollViewHeight);
        
        NSString* imgUrl = _newsModel.imgUrl;
        NetEaseImgView* imgView = [[NetEaseImgView alloc] initWithFrame:CGRectMake(0, 0, kWidth, scrollViewHeight)];
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        if (imgUrl) {
            [imgView sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
        } else {            //如果图片不存在，就用默认的 有 “网易” 字样的图片
            [imgView setBgImg];
        }
        
        [_scrollView addSubview:imgView];
        
        
        //新闻简介
        NSString* contentStr = _newsModel.title;
        _newsContentLabel.text = contentStr;
        
        //pageControl
        _pageControl.hidden = YES;
    }
}

//结束减速运动时，切换
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    //计算当前的页面序号
    NSInteger index = scrollView.contentOffset.x/kWidth;
    //切换文本显示
    NSString* contentStr = [_headerNewsArray[index] objectForKey:@"title"];
    _newsContentLabel.text = contentStr;
    //切换pagecontrol显示
    _pageControl.currentPage = index;
}

- (void)pushToPhotoSetVCAction {
    //通过响应者链,找到newsVC
    NewsViewController* newsVC = [Tools className:@"NewsViewController" fromResponder:self.nextResponder];
    if (newsVC) {
        NSString* skipID = _newsModel.skipID;
        NSString* newsId = _newsModel.newsId;
        
        if (skipID.length) {           //进图集
            PhotoShowViewController* photoShowVC = [[PhotoShowViewController alloc] init];
            photoShowVC.photoSetId = skipID;
            [newsVC.navigationController pushViewController:photoShowVC animated:YES];
            
        } else if (newsId.length) {     //进新闻详情
            NewsDetailViewController* newsDetailVC = [[NewsDetailViewController alloc] init];
            newsDetailVC.newsId = newsId;
            [newsVC.navigationController pushViewController:newsDetailVC animated:YES];
        }
    }
}

@end
